home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / exampleCode / GLX / texvol / read.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  11.9 KB  |  502 lines

  1. /*
  2.  * Copyright (C) 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. /*----------------------------------------------------------------------------
  18.  *
  19.  * file   : read.c
  20.  *
  21.  * Author : Yusuf Attarwala
  22.  * Date   : Sep 93
  23.  *
  24.  *---------------------------------------------------------------------------*/
  25. #include <sys/types.h>
  26. #include <malloc.h>
  27. #include <sys/stat.h>
  28. #include <fcntl.h>
  29. #include <math.h>
  30. #include <stdio.h>
  31. #include <string.h>
  32. #include <stdlib.h>
  33.  
  34. #include "globals_vol.h"
  35.  
  36. void readAndInitData();
  37. void initTLut();
  38.  
  39. void
  40. computeBoundingBox()
  41. {
  42.  
  43.     int i;
  44.     float cenx,ceny,cenz;
  45.  
  46.     bminx = -(float)width/2.0;
  47.     bmaxx =  (float)width/2.0;
  48.     bminy = -(float)height/2.0;
  49.     bmaxy =  (float)height/2.0;
  50.     bminz = -(float)depth/2.0;
  51.     bmaxz =  (float)depth/2.0;
  52.  
  53.     bdiag = sqrt((bmaxx)*(bmaxx)+(bmaxy)*(bmaxy)+(bmaxz)*(bmaxz));
  54.  
  55.     cenx = (bmaxx+bminx)/2.0;
  56.     ceny = (bmaxy+bminy)/2.0;
  57.     cenz = (bmaxz+bminz)/2.0;
  58.  
  59.     /* compute arbitrary clipping plane variables */
  60.     for (i=0;i<6;i++) {
  61.     clip[i].xyz[_X] = cenx;
  62.     clip[i].xyz[_Y] = ceny;
  63.     clip[i].xyz[_Z] = cenz;
  64.     }
  65.  
  66.     /* +x plane */
  67.     clip[0].xyz[_X] = bminx - 0.01*(bmaxx-bminx);
  68.     /* -x plane */
  69.     clip[1].xyz[_X] = bmaxx + 0.01*(bmaxx-bminx);
  70.     /* +y plane */
  71.     clip[2].xyz[_Y] = bminy - 0.01*(bmaxy-bminy);
  72.     /* -y plane */
  73.     clip[3].xyz[_Y] = bmaxy + 0.01*(bmaxy-bminy);
  74.     /* +z plane */
  75.     clip[4].xyz[_Z] = bminz - 0.01*(bmaxz-bminz);
  76.     /* -z plane */
  77.     clip[5].xyz[_Z] = bmaxz + 0.01*(bmaxz-bminz);
  78.     for (i=0;i<6;i++) {
  79.     /* -P dot N */
  80.     clip[i].coeff[3] = -(
  81.         clip[i].coeff[0]*clip[i].xyz[_X] +
  82.         clip[i].coeff[1]*clip[i].xyz[_Y] +
  83.         clip[i].coeff[2]*clip[i].xyz[_Z] );
  84.     }
  85.  
  86.     /* set the limits */
  87.  
  88.     clip[0].min = clip[0].xyz[_X];
  89.     clip[0].max = clip[1].xyz[_X];
  90.  
  91.     clip[1].min = clip[0].xyz[_X];
  92.     clip[1].max = clip[1].xyz[_X];
  93.  
  94.     clip[2].min = clip[2].xyz[_Y];
  95.     clip[2].max = clip[3].xyz[_Y];
  96.  
  97.     clip[3].min = clip[2].xyz[_Y];
  98.     clip[3].max = clip[3].xyz[_Y];
  99.  
  100.     clip[4].min = clip[4].xyz[_Z];
  101.     clip[4].max = clip[5].xyz[_Z];
  102.  
  103.     clip[5].min = clip[4].xyz[_Z];
  104.     clip[5].max = clip[5].xyz[_Z];
  105. }
  106.  
  107.  
  108. unsigned char *voxels;
  109. unsigned char *vptr;
  110. unsigned char *wptr;
  111. unsigned long *xy_voxels;   /* xy planes */
  112. unsigned long *xyPtr;
  113.  
  114. unsigned long *yz_voxels;    /* yz planes */
  115. unsigned long *yzPtr;
  116.  
  117. unsigned long *zx_voxels;    /* zx planes */
  118. unsigned long *zxPtr;
  119.  
  120. #define SZ(x) sizeof(x)/sizeof(float)
  121. static float tex[] = { TX_MAGFILTER, TX_BILINEAR,
  122.                        TX_MINFILTER, TX_BILINEAR,
  123.                        TX_WRAP,      TX_REPEAT,
  124.                        TX_WRAP_S,    TX_REPEAT,
  125.                        TX_WRAP_T,    TX_REPEAT,
  126.                        TX_NULL,
  127.                      };
  128. static float env[] = { TV_MODULATE,
  129.                        TV_NULL,
  130.                      };
  131.  
  132. Boolean prepare2DTexture(),
  133.         prepare3DTexture();
  134.  
  135. void
  136. readAndInitData(fname,w,h,d,threshold)
  137. char *fname;
  138. int w,h,d;
  139. int threshold;
  140. {
  141.     int fd;
  142.  
  143.     width  = w;
  144.     height = h;
  145.     depth  = d;
  146.  
  147.     vptr = voxels   = (unsigned char *) malloc((width*height*depth));
  148.  
  149.     if ( (fd = open(fname, O_RDONLY)) < 0 ) {
  150.         perror("volTex: can't open file");
  151.         exit(-1);
  152.     }
  153.  
  154.     read(fd, voxels, width*height*depth );
  155.     close( fd );
  156.     fprintf(stderr,"volTex : reading finished\n");
  157.  
  158.  
  159.     /* define the polygon dimensions on which the texture will be mapped */
  160.     /* xy plane */
  161.     xy_v0[0] = -width/2.0;
  162.     xy_v0[1] = -height/2.0;
  163.     xy_v0[2] = 0.0;
  164.  
  165.     xy_v1[0] =  width/2.0;
  166.     xy_v1[1] = -height/2.0;
  167.     xy_v1[2] = 0.0;
  168.  
  169.     xy_v2[0] =  width/2.0;
  170.     xy_v2[1] =  height/2.0;
  171.     xy_v2[2] = 0.0;
  172.  
  173.     xy_v3[0] = -width/2.0;
  174.     xy_v3[1] =  height/2.0;
  175.     xy_v3[2] = 0.0;
  176.  
  177.     /* yz plane */
  178.     yz_v0[0] = 0.0;
  179.     yz_v0[1] = -height/2.0;
  180.     yz_v0[2] = -depth/2.0;
  181.  
  182.     yz_v1[0] = 0.0;
  183.     yz_v1[1] = height/2.0;
  184.     yz_v1[2] = -depth/2.0;
  185.  
  186.     yz_v2[0] = 0.0;
  187.     yz_v2[1] = height/2.0;
  188.     yz_v2[2] = depth/2.0;
  189.  
  190.     yz_v3[0] = 0.0;
  191.     yz_v3[1] = -height/2.0;
  192.     yz_v3[2] = depth/2.0;
  193.  
  194.     /* zx plane */
  195.     zx_v0[0] = -width/2.0;
  196.     zx_v0[1] = 0.0;
  197.     zx_v0[2] = -depth/2.0;
  198.  
  199.     zx_v1[0] = -width/2.0;
  200.     zx_v1[1] = 0.0;
  201.     zx_v1[2] = depth/2.0;
  202.  
  203.     zx_v2[0] =  width/2.0;
  204.     zx_v2[1] = 0.0;
  205.     zx_v2[2] = depth/2.0;
  206.  
  207.     zx_v3[0] = width/2.0;
  208.     zx_v3[1] = 0.0;
  209.     zx_v3[2] = -depth/2.0;
  210. }
  211.  
  212. Boolean
  213. prepare2DTexture()
  214. {
  215.  
  216.     int i,j,k;
  217.     unsigned char *qptr, *qp;
  218.  
  219.     /* flip the images */
  220.     qp = qptr = (unsigned char *) malloc( (width*height*depth));
  221.     if (!qp) {
  222.     fprintf(stderr,"vol2d : could not allocate memory \n");
  223.     exit(2);
  224.     }
  225.     memcpy(qptr,voxels,width*height*depth);
  226.  
  227.     j    = width*height*depth;
  228.     vptr = voxels+width*height*depth;
  229.     for (i=0;i<j;i++) {
  230.  
  231. #ifdef LATER
  232.     /* apply threshold here */
  233.         if (threshold) {
  234.         if (threshold < 0) {
  235.             if (*qptr > -threshold) *qptr = 0;
  236.                 *qptr *= 2.0;
  237.         }
  238.         else {
  239.             if (*qptr < threshold) *qptr = 0;
  240.         }
  241.  
  242.             if (*qptr > 256) *qptr = 256;
  243.         if (*qptr < 0)   *qptr = 0;
  244.         }
  245. #endif
  246.  
  247.         *vptr-- = *qptr++;
  248.     }
  249.     if (qp) free(qp);
  250.  
  251.     /* stuff this data into a two component texture */
  252.  
  253.     /* xy plane */
  254.  
  255.     printf("setting up xy textures...\n");
  256.  
  257.     xyPtr = xy_voxels = (unsigned long *) malloc((width*height*depth/2)
  258.                         *sizeof(unsigned long));
  259.  
  260.     for (i=0;i<depth;i++) {
  261.         for (j=0;j<height;j++) {
  262.             for (k=0;k<width; k+=2) {
  263.                 vptr = voxels + i*width*height + j*width + k;
  264.                 *xyPtr = 0;
  265.                 *xyPtr++ = ((*vptr)<<24)     | ((*(vptr))<<16) | 
  266.                            ((*(vptr+1))<<8)  | ((*(vptr+1)));
  267.             }
  268.         }
  269.     }
  270.  
  271. /* the unopti uses too many texbinds, each slice is one texture,
  272.    whereas opti stuffs all the slices of each set on one texture */
  273.  
  274. #ifdef OPTI
  275.     for (i=0;i<depth;i++) {
  276.         texdef2d(i+1,2,width,height,(xy_voxels + i*(width*height/2)),SZ(tex),tex);
  277.     }
  278. #else 
  279.     texdef2d(100,2,width,height*depth,xy_voxels,SZ(tex),tex);
  280. #endif
  281.     free(xy_voxels);
  282.  
  283.     /* yz plane */
  284.  
  285.     printf("setting up yz textures...\n");
  286.  
  287.     yzPtr = yz_voxels = (unsigned long *) malloc((width*height*depth/2)
  288.                         *sizeof(unsigned long));
  289.     for (i=0;i<width;i++) {
  290.         for (j=0;j<depth;j++) {
  291.             for (k=0;k<height; k+=2) {
  292.                 vptr = voxels + (width*height)*(depth-1-j) + k*width + (width-i);
  293.                 wptr = voxels + (width*height)*(depth-1-j) + (k+1)*width + (width-i);
  294.                 *yzPtr = 0;
  295.                 *yzPtr++ = ((*vptr)<<24) | ((*(vptr))<<16) | 
  296.                            ((*wptr)<<8)  | (*wptr);
  297.             }
  298.         }
  299.     }
  300.  
  301. #ifdef OPTI
  302.     for (i=0;i<width;i++) {
  303.         texdef2d(400+i+1,2,height,depth,(yz_voxels + i*(height*depth/2)),SZ(tex),
  304.          tex);
  305.     }
  306. #else
  307.     texdef2d(400,2,height,depth*width,yz_voxels,SZ(tex),tex);
  308. #endif
  309.     free(yz_voxels);
  310.  
  311.     /* zx plane */
  312.  
  313.     printf("setting up zx textures...\n");
  314.  
  315.     zxPtr = zx_voxels = (unsigned long *) malloc((width*height*depth/2)
  316.                         *sizeof(unsigned long));
  317.     for (i=0;i<height;i++) {
  318.         for (j=0;j<width;j++) {
  319.             for (k=0;k<depth; k+=2) {
  320.                 vptr = voxels + (width*height)*(depth-1-k) + width*(height-1-i) + j;
  321.                 wptr = voxels + (width*height)*(depth-1-k-1) + width*(height-1-i) + j;
  322.                 *zxPtr = 0;
  323.                 *zxPtr++ = ((*vptr)<<24) | ((*(vptr))<<16) | 
  324.                            ((*wptr)<<8)  | (*wptr);
  325.             }
  326.         } 
  327.     } 
  328.  
  329. #ifdef OPTI
  330.     for (i=0;i<height;i++) {
  331.         texdef2d(800+i+1,2,depth,width,(zx_voxels + i*(depth*width/2)),SZ(tex),
  332.          tex);
  333.     }
  334. #else 
  335.     texdef2d(800,2,depth,width*height,zx_voxels,SZ(tex),tex);
  336. #endif
  337.     free(zx_voxels);
  338.  
  339.     free(voxels);
  340.  
  341.     tevdef  (1,SZ(env),env);
  342.     tevbind(TV_ENV0,1);
  343.  
  344.     return(1);
  345. }
  346.  
  347. /* 8 bit texture */
  348. static float texProps8[] = {TX_MAGFILTER,       TX_TRILINEAR,
  349.                             TX_MINFILTER,       TX_TRILINEAR,
  350.                             TX_WRAP,            TX_CLAMP,
  351.                             TX_INTERNAL_FORMAT, TX_IA_8,
  352.                             TX_NULL,
  353.                            };
  354.  
  355. /* 4 bit texture */
  356. static float texProps4[] = {TX_MAGFILTER,       TX_TRILINEAR,
  357.                             TX_MINFILTER,       TX_TRILINEAR,
  358.                             TX_WRAP,            TX_CLAMP,
  359. /*
  360.                             TX_INTERNAL_FORMAT, TX_RGBA_4,
  361. */
  362.                             TX_NULL,
  363.                            };
  364.  
  365.  
  366. static float envProps[] = { TV_MODULATE,
  367.                             /*TV_COLOR,0.75,0.13,0.06,1.,TV_BLEND,*/
  368.                              TV_NULL,
  369.                            };
  370.  
  371. /* properties for texture look up table */
  372. static float   tlutProps[] = { TL_NULL };
  373. static u_long  tlut[256];
  374.  
  375. /* XXX3 */
  376.  
  377. Boolean
  378. prepare3DTexture()
  379. {
  380.     /* define a 3d texture, one component */
  381.     int i,j,k;
  382.     unsigned char *qptr, *qp;
  383.  
  384.     /* flip the images */
  385.     qp = qptr = (unsigned char *) malloc( (width*height*depth));
  386.     if (!qp) {
  387.     fprintf(stderr,"vol2d : could not allocate memory \n");
  388.     exit(2);
  389.     }
  390.     memcpy(qptr,voxels,width*height*depth);
  391.  
  392.     j    = width*height*depth;
  393.     vptr = voxels+width*height*depth;
  394.     for (i=0;i<j;i++) {
  395.  
  396. #ifdef LATER
  397.     /* apply threshold here */
  398.         if (threshold) {
  399.         if (threshold < 0) {
  400.             if (*qptr > -threshold) *qptr = 0;
  401.                 *qptr *= 2.0;
  402.         }
  403.         else {
  404.             if (*qptr < threshold) *qptr = 0;
  405.         }
  406.  
  407.             if (*qptr > 256) *qptr = 256;
  408.         if (*qptr < 0)   *qptr = 0;
  409.         }
  410. #endif
  411.  
  412.         *vptr-- = *qptr++;
  413.     }
  414.     if (qp) free(qp);
  415.  
  416.     printf("setting up 3d textures...\n");
  417.  
  418.     texdef3d(
  419.              1,              /* texture Id */
  420.              1,              /* number of components */
  421.              width,          /* width */
  422.              height,         /* height */
  423.              depth,          /* depth */
  424.              (unsigned long *) voxels,
  425. #ifdef NO
  426.              SZ(texProps4),  /* np */
  427. #endif
  428.              0,
  429.              texProps4 );
  430.  
  431.     initTLut(1);   
  432.     tlutbind(0, 1);
  433.  
  434. #ifdef TEXGEN
  435.     {
  436.     static float    S_plane[] = { 1., 0., 0., .5 };
  437.     static float    T_plane[] = { 0., 1., 0., .5 };
  438.     static float    R_plane[] = { 0., 0., 1., .5 };
  439.     static float    Q_plane[] = { 0., 0., 0., 1. };
  440.     
  441.     
  442.     texgen(TX_S, TG_LINEAR, S_plane);
  443.     texgen(TX_T, TG_LINEAR, T_plane);
  444.     texgen(TX_R, TG_LINEAR, R_plane);
  445.     texgen(TX_Q, TG_LINEAR, Q_plane);
  446.     
  447.     texgen(TX_S, TG_ON, S_plane);
  448.     texgen(TX_T, TG_ON, T_plane);
  449.     texgen(TX_R, TG_ON, R_plane);
  450.     texgen(TX_Q, TG_ON, Q_plane);
  451.     }
  452. #endif
  453.  
  454.     tevdef  (1,SZ(envProps),envProps);
  455.     tevbind(TV_ENV0,1);
  456.  
  457.     texbind(TX_TEXTURE_0,1);
  458.  
  459.     return(1);
  460. }
  461.  
  462. void
  463. initTLut(indx)
  464. int indx;
  465. {
  466.     int i,j;
  467.     long t[256][4];
  468.     for (i=0;i<256;i++) {
  469.         for(j=0;j<4;j++){
  470.             t[i][j] = i;
  471.         }
  472.     }
  473.  
  474.     for(i=0;i<256;i++){
  475.         tlut[i] = (long) (
  476.                        (long) ((t[i][3] << 24) & 0xff000000) |
  477.                        (long) ((t[i][2] << 16) & 0x00ff0000) |
  478.                        (long) ((t[i][1] << 8)  & 0x0000ff00) |
  479.                        (long) ((t[i][0]     )  & 0x000000ff));
  480.     }
  481.  
  482.     tlutdef(indx, 4, 256, tlut, 0, tlutProps);
  483. }
  484.  
  485.  
  486.  
  487. void
  488. outofDegenerate()
  489. {
  490.     drawMode = TEXTURED;
  491.     refresh = 1;
  492.     drawScene();
  493. }
  494.  
  495. void
  496. intoDegenerate()
  497. {
  498.     drawMode = WIREFRAME;
  499.     refresh = 1;
  500.     drawScene();
  501. }
  502.